<?php
require_once ($_SERVER["DOCUMENT_ROOT"] ."/include/config.php");
require_once ("include/dict.php");
require_once ("include/connection_controller.php");
require_once ("include/cable_controller.php");
$cables = CableController::GetCablesListArray();

$dict = new DictionaryController($config);
$types = $dict -> GetDictionary("support_type", false);
$id = $_GET["id"];
if (isset($id)) {
	$segment = ConnectionController::GetSegment($id);
	$segment -> start_point = json_decode($segment -> start_point);
	if (strlen($segment -> start_point[1]) == 0)
		$segment -> start_point[1] = "[нет названия]";
	$segment -> end_point = json_decode($segment -> end_point);
	if (strlen($segment -> end_point[1]) == 0)
		$segment -> end_point[1] = "[нет названия]";
}
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script src="http://api-maps.yandex.ru/2.0.30/?load=package.map&lang=ru-RU" type="text/javascript"></script>
		<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>
		<script src="js/json.js" type='text/javascript'></script>
		<script src="js/jquery-ui.min.js" type="text/javascript"></script>
		<script src="js/util_script.js" type="text/javascript"></script>
		<script src="js/jquery.editable.js" type="text/javascript"></script>
		<script src="js/connection.js" type='text/javascript'></script>
		<link href="css/styles.css" rel="stylesheet" type="text/css" />
		<link href="css/jquery-ui.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript">var segmentId =<?= isset($_GET["id"]) ? "'" . $_GET["id"] . "'" : "null" ?>;
		ymaps.ready(function() {
            var length = ((ymaps.coordSystem.geo.getDistance(<?= $segment->coordinates ?>))).toFixed(2);
			$("#segment-length").text(length + " м");
            <? if ($segment->attenuation) : ?>
            $("#segment-attenuation").text((length/1000 * <?= $segment->attenuation ?>).toFixed(2) + " дб");
            <? else : ?>
            $("#segment-attenuation").text("-");
            <? endif; ?>
		})	;			

	$(document).ready(function() {
		$("button").button();
		$("a.point-link").click(function() {
			var id = $(this).data("id");
			parent.$("body").trigger({type: "openPoint", id:id });
		});
		$("span.editable_single").editable();
		
		$("#save-segment").click(saveSegment);
		$("#delete-segment").click(deleteSegment);
		
		
		<?php
		if (isset($segment))
			echo "$('#segment-cable').val('$segment->id_cable');";
 		?>
	});
		</script>
	</head>
	<body style="font-family: Verdana, sans-serif; font-size: 80%; ">

		<table width="100%" cellspacing="10" style='table-layout: fixed'>
			<tr>
				<td width="50%"><b>Расчетная длина:</b></td>
				<td><span id="segment-length"></span></td>
			</tr>
			<tr>
				<td><b>Расчетное затухание:</b></td>
				<td><span id="segment-attenuation"></span></td>
			</tr>
			<tr>
				<td><b>Начальная точка:</b></td>
				<td><a href="javascript:void(0)" class="point-link" data-id="<?= $segment -> start_point[0] ?>"><?= $segment -> start_point[1] ?></a></td>
			</tr>
			<tr>
				<td><b>Конечная точка:</b></td>
				<td><a href="javascript:void(0)" class="point-link" data-id="<?= $segment -> end_point[0] ?>"><?= $segment -> end_point[1] ?></a></td>
			</tr
			<tr>
				<td><b>Используемый кабель:</b></td>
				<td><select id="segment-cable">
					<?php foreach ($cables as $id => $title) :?>
						<option value="<?= $id ?>"><?= $title ?></option>
					<?php endforeach; ?>
				</select></td>
			</tr>
			<tr>
				<td><b>Фактическая длина:</b></td>
				<td><span id="segment-factlength" class="editable_single"><?= $segment -> length ?></span></td>
			</tr>
			<tr>
				<td><b>Фактическое затухание на участке:</b></td>
				<td><span id="segment-factattenuation" class="editable_single"><?= $segment -> fact_attenuation ?></span>
				</td>
			</tr>
			<tr><td colspan='2'><br/>
				<br/>
				<hr/>
				<br/></td></tr>
			<tr>
				<td align="left">
				<button id="delete-segment">
					Удалить
				</button></td>
				<td align="right">
				<button id="save-segment">
					Сохранить
				</button></td>
			</tr>
		</table>
	</body>
</html>
